package com.gzc.just.play.last.war.sceneserver.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import com.gzc.just.play.last.war.common.handler.VirtualThreadServerFramework;
import jakarta.annotation.PostConstruct;

/**
 * 场景服务器配置类
 * 对应C++的SSSConfig结构体，管理场景服务器的所有配置参数
 */
@Component
@Configuration
public class SceneConfig {
    private static final Logger logger = LoggerFactory.getLogger(SceneConfig.class);
    
    // 服务器基本配置
    @Value("${scene.server.id:1}")
    private int sceneServerId;
    
    @Value("${scene.server.name:SceneServer}")
    private String sceneServerName;
    
    // 中心服务器连接配置
    @Value("${central.server.ip:127.0.0.1}")
    private String centralServerIp;
    
    @Value("${central.server.port:30003}")
    private int centralServerPort;
    
    @Value("${central.server.max.msg.size:1024000}")
    private int centralServerMaxMsgSize;
    
    // 网关服务器监听配置
    @Value("${gate.server.listen.ip:0.0.0.0}")
    private String gateServerListenIp;
    
    @Value("${gate.server.listen.port:40002}")
    private int gateServerListenPort;
    
    @Value("${gate.server.max.msg.size:1024000}")
    private int gateServerMaxMsgSize;
    
    // 网关服务器认证配置
    @Value("${gate.server.password:defaultpassword}")
    private String gateServerPassword;
    
    // 日志服务器配置
    @Value("${log.server.ip:127.0.0.1}")
    private String logServerIp;
    
    @Value("${log.server.port:50001}")
    private int logServerPort;
    
    // 战斗相关配置
    @Value("${battle.npc.max.lifetime:60000}")
    private long npcMaxLifeTime;
    
    @Value("${battle.finish.protect.time:60000}")
    private long battleFinishProtectTimeSpan;
    
    @Value("${battle.max.count:1000}")
    private int maxBattleCount;
    
    // 性能配置
    @Value("${performance.thread.num:4}")
    private int threadNum;
    
    @Value("${performance.mini.battle.obj.num:100}")
    private int miniBattleObjNum;
    
    @Value("${performance.max.statis.msg.to.show:5}")
    private int maxStatisMsgToShow;
    
    @Value("${performance.msg.statics.interval:60000}")
    private long msgStaticsInterval;
    
    @Value("${performance.enable.statics:true}")
    private boolean enableStatics;
    
    @Value("${performance.max.player.per.battle:10}")
    private int maxPlayerPerBattle;
    
    // 战斗AI配置
    @Value("${ai.enabled:true}")
    private boolean aiEnabled;
    
    @Value("${ai.robot.enabled:true}")
    private boolean robotEnabled;
    
    @Value("${ai.update.interval:100}")
    private long aiUpdateInterval;
    
    @Value("${ai.robot.think.interval:1000}")
    private long robotThinkInterval;
    
    // 场景配置
    @Value("${scene.default.map:1}")
    private int defaultMapId;
    
    @Value("${scene.max.map.width:1000}")
    private int maxMapWidth;
    
    @Value("${scene.max.map.height:1000}")
    private int maxMapHeight;
    
    @Value("${scene.pathfinding.enabled:true}")
    private boolean pathfindingEnabled;
    
    @PostConstruct
    public void init() {
        logger.info("SceneConfig initialized:");
        logger.info("- SceneServer ID: {}, Name: {}", sceneServerId, sceneServerName);
        logger.info("- Central Server: {}:{}", centralServerIp, centralServerPort);
        logger.info("- Gate Server Listen: {}:{}", gateServerListenIp, gateServerListenPort);
        logger.info("- Log Server: {}:{}", logServerIp, logServerPort);
        logger.info("- Battle Config: NPC Lifetime: {}ms, Battle Finish Protect: {}ms", 
                   npcMaxLifeTime, battleFinishProtectTimeSpan);
        logger.info("- Performance Config: Thread Num: {}, Max Battle Count: {}", 
                   threadNum, maxBattleCount);
        logger.info("- AI Config: Enabled: {}, Robot Enabled: {}", aiEnabled, robotEnabled);
        logger.info("- Scene Config: Default Map: {}, Pathfinding: {}", 
                   defaultMapId, pathfindingEnabled);
    }
    
    // Getter方法
    public int getSceneServerId() {
        return sceneServerId;
    }
    
    public String getSceneServerName() {
        return sceneServerName;
    }
    
    public String getCentralServerIp() {
        return centralServerIp;
    }
    
    public int getCentralServerPort() {
        return centralServerPort;
    }
    
    public int getCentralServerMaxMsgSize() {
        return centralServerMaxMsgSize;
    }
    
    public String getGateServerListenIp() {
        return gateServerListenIp;
    }
    
    public int getGateServerListenPort() {
        return gateServerListenPort;
    }
    
    public int getGateServerMaxMsgSize() {
        return gateServerMaxMsgSize;
    }
    
    public String getGateServerPassword() {
        return gateServerPassword;
    }
    
    public String getLogServerIp() {
        return logServerIp;
    }
    
    public int getLogServerPort() {
        return logServerPort;
    }
    
    public long getNpcMaxLifeTime() {
        return npcMaxLifeTime;
    }
    
    public long getBattleFinishProtectTimeSpan() {
        return battleFinishProtectTimeSpan;
    }
    
    public int getMaxBattleCount() {
        return maxBattleCount;
    }
    
    public int getThreadNum() {
        return threadNum;
    }
    
    public int getMiniBattleObjNum() {
        return miniBattleObjNum;
    }
    
    public int getMaxStatisMsgToShow() {
        return maxStatisMsgToShow;
    }
    
    public long getMsgStaticsInterval() {
        return msgStaticsInterval;
    }
    
    public boolean isEnableStatics() {
        return enableStatics;
    }
    
    public int getMaxPlayerPerBattle() {
        return maxPlayerPerBattle;
    }
    
    public boolean isAiEnabled() {
        return aiEnabled;
    }
    
    public boolean isRobotEnabled() {
        return robotEnabled;
    }
    
    public long getAiUpdateInterval() {
        return aiUpdateInterval;
    }
    
    public long getRobotThinkInterval() {
        return robotThinkInterval;
    }
    
    public int getDefaultMapId() {
        return defaultMapId;
    }
    
    public int getMaxMapWidth() {
        return maxMapWidth;
    }
    
    public int getMaxMapHeight() {
        return maxMapHeight;
    }
    
    public boolean isPathfindingEnabled() {
        return pathfindingEnabled;
    }

    @Bean
    public VirtualThreadServerFramework.ServerType sceneServerType() {
        return VirtualThreadServerFramework.ServerType.SCENE_SERVER;
    }
}